package online.sixfish.doubao.controller.admin;

import online.sixfish.common.api.ApiResult;
import online.sixfish.common.dto.PageDTO;
import online.sixfish.doubao.service.ArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("/admin/article")
public class AdminArticeController {
    @Resource
    ArticleService articleService;

    @GetMapping("/list")
    @PreAuthorize("hasRole('admin')")
    public ApiResult list(@RequestParam(required = false) Integer type,
                          @RequestParam(required = false) Integer sortType,
                          @RequestParam(required = false) String title,
                          @RequestParam(name = "pageNum") Integer pageNum,
                          @RequestParam(name = "pageSize",required = false,defaultValue = "5") Integer pageSize) {
        PageDTO pageDTO = articleService.select(type, sortType, title, pageNum, pageSize);
        return ApiResult.success(pageDTO);

    }

    @PostMapping("/delete/{id}")
    @PreAuthorize("hasRole('admin')")
    public ApiResult deleteArticle(@PathVariable String id){
        articleService.deleteArticle(id);
        return ApiResult.success();
    }

    @PostMapping("/top/{id}")
    @PreAuthorize("hasRole('admin')")
    public ApiResult updateArticleTop(@PathVariable String id,
                                      @RequestParam Integer isTop){
        articleService.updateArticleTop(id,isTop);
        return ApiResult.success();
    }
}
